<template>
  <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
    <el-form :model="addBaseDepartmentForm" ref="addBaseDepartmentRef" :rules="addBaseDepartmentrules">
      <el-form-item label="年级主任" :label-width="formLabelWidth" prop="name" v-if="type == 2">
        <el-select v-model="addBaseDepartmentForm.leader1" filterable placeholder="请选择年级主任">
          <el-option v-for="item in list" :key="item.teacherNumber" :label="item.name" :value="item.teacherNumber" />
        </el-select>
      </el-form-item>

      <el-form-item label="中心主任" :label-width="formLabelWidth" prop="name">
        <el-select v-model="addBaseDepartmentForm.leader2" filterable placeholder="请选择中心主任">
          <el-option v-for="item in list" :key="item.teacherNumber" :label="item.name" :value="item.teacherNumber" />
        </el-select>
      </el-form-item>

      <el-form-item label="主管校长" :label-width="formLabelWidth" prop="name">
        <el-select v-model="addBaseDepartmentForm.leader3" filterable placeholder="请选择主管校长">
          <el-option v-for="item in list" :key="item.teacherNumber" :label="item.name" :value="item.teacherNumber" />
        </el-select>
      </el-form-item>
      <el-form-item label="校区校长" :label-width="formLabelWidth" prop="name">
        <el-select v-model="addBaseDepartmentForm.leader4" filterable placeholder="请选择校区校长">
          <el-option v-for="item in list" :key="item.teacherNumber" :label="item.name" :value="item.teacherNumber" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
  name: "AddBaseDepartment",
  components: { ElForm, ElMessage },
  data() {
    return {
      name: "",
      placeholder: "",
      dialogFormVisible: ref(false),
      formLabelWidth: "120px",
      title: "",
      list: [],
      personnelType: "",
      addBaseDepartmentForm: {
        leader4: "",
        leader3: "",
        leader2: "",
        leader1: "",
        parentId: "",
        name: "",
        orderNum: "",
        type: "",
        campus: "",
      },
      // 校验规则
      addBaseDepartmentrules: {
        leader4: vxRule(true, null, "blur"),
        leader3: vxRule(true, null, "blur"),
        leader2: vxRule(true, null, "blur"),
        leader1: vxRule(true, null, "blur"),
        name: vxRule(true, null, "blur"),
      },
    };
  },
  methods: {
    closeDialog() {
      this.clear();
    },
    cancel() {
      this.dialogFormVisible = false;
      this.clear();
    },
    getBaseDepartmentById(id) {
      httpService("/javaApi/baseDepartment/queryById", { id: id }, "get").then(
        (data) => {
          this.addBaseDepartmentForm = data;
        }
      );
    },
    clear() {
      this.dialogFormVisible = false;
      //清空form
      this.addBaseDepartmentForm.leader4 = "";
      this.addBaseDepartmentForm.leader3 = "";
      this.addBaseDepartmentForm.leader2 = "";
      this.addBaseDepartmentForm.leader1 = "";
      this.addBaseDepartmentForm.id = "";
      this.addBaseDepartmentForm.parentId = "";
      this.addBaseDepartmentForm.name = "";
      this.addBaseDepartmentForm.orderNum = "";
      this.addBaseDepartmentForm.type = "";
      this.addBaseDepartmentForm.campus = "";
    },
    getBaseTeacherListApp() {
      httpService(
        "/javaApi/baseTeacher/list/1/10000",
        {
        },
        "get"
      ).then((data) => {
        this.list = data.list;
      });
    },
    save() {
      this.$refs.addBaseDepartmentRef.validate((valid) => {
        if (valid) {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("name", this.addBaseDepartmentForm.name);
          params.append("orderNum", this.addBaseDepartmentForm.orderNum);
          params.append("type", this.addBaseDepartmentForm.type);
          params.append("leader1", this.addBaseDepartmentForm.leader1);
          params.append("leader2", this.addBaseDepartmentForm.leader2);
          params.append("leader3", this.addBaseDepartmentForm.leader3);
          params.append("leader4", this.addBaseDepartmentForm.leader4);

          if (this.addBaseDepartmentForm.id) {
            params.append("id", this.addBaseDepartmentForm.id);
            httpService("/javaApi/baseDepartment/edit", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success(data.object);
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getBaseDepartmentList();
                } else {
                  ElMessage.error(data.message);
                }
              }
            );
          } else {
            httpService("/javaApi/baseDepartment/add", params, "post").then(
              (data) => {
                if (data.code == 200) {
                  ElMessage.success(data.object);
                  this.clear();
                  //刷新父组件的数据
                  this.$parent.getBaseDepartmentList();
                } else {
                  ElMessage.error(data.message);
                }
              }
            );
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style></style>